home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / print / gs261sr1.zip / GENARCH.C < prev    next >
C/C++ Source or Header  |  1993-05-12  |  5KB  |  114 lines

  1. /* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* genarch.c */
  20. /* Generate a header file (arch.h) with parameters */
  21. /* reflecting the machine architecture and compiler characteristics. */
  22.  
  23. #include <stdio.h>
  24.  
  25. /* We should write the result on stdout, but the original Turbo C 'make' */
  26. /* can't handle output redirection (sigh). */
  27.  
  28. main(argc, argv)
  29.     int argc;
  30.     char *argv[];
  31. {    char *fname = argv[1];
  32.     long one = 1;
  33. #define ffs_strlen 16
  34.     char *ffs = "ffffffffffffffff";
  35.     struct { char c; short s; } ss;
  36.     struct { char c; int i; } si;
  37.     struct { char c; long l; } sl;
  38.     struct { char c; char *p; } sp;
  39.     struct { char c; float f; } sf;
  40.     struct { char c; double d; } sd;
  41.     long lm1 = -1;
  42.     long lr1 = lm1 >> 1, lr2 = lm1 >> 2;
  43.     unsigned long um1 = ~(unsigned long)0;
  44.     int im1 = -1;
  45.     int ir1 = im1 >> 1, ir2 = im1 >> 2;
  46.     union { long l; char *p; } pl0, pl1;
  47.     int ars;
  48.     int lwidth = sizeof(long) * 8;
  49.     union { float f; int i; long l; } f0, f1, fm1;
  50.     FILE *f = fopen(fname, "w");
  51.     if ( f == NULL )
  52.        {    fprintf(stderr, "genarch.c: can't open %s for writing\n", fname);
  53.         exit(1);
  54.        }
  55.     fprintf(f, "/* Parameters derived from machine and compiler architecture */\n");
  56.  
  57.     fprintf(f, "\n\t/* Scalar alignments */\n");
  58. #define offset(s, e) (int)((char *)&s.e - (char *)&s)
  59.     fprintf(f, "#define arch_align_short_mod %d\n", offset(ss, s));
  60.     fprintf(f, "#define arch_align_int_mod %d\n", offset(si, i));
  61.     fprintf(f, "#define arch_align_long_mod %d\n", offset(sl, l));
  62.     fprintf(f, "#define arch_align_ptr_mod %d\n", offset(sp, p));
  63.     fprintf(f, "#define arch_align_float_mod %d\n", offset(sf, f));
  64.     fprintf(f, "#define arch_align_double_mod %d\n", offset(sd, d));
  65. #undef offset
  66.  
  67.     fprintf(f, "\n\t/* Scalar sizes */\n");
  68.     fprintf(f, "#define arch_sizeof_short %d\n", sizeof(short));
  69.     fprintf(f, "#define arch_sizeof_int %d\n", sizeof(int));
  70.     fprintf(f, "#define arch_sizeof_long %d\n", sizeof(long));
  71.     fprintf(f, "#define arch_sizeof_ptr %d\n", sizeof(char *));
  72.     fprintf(f, "#define arch_sizeof_float %d\n", sizeof(float));
  73.     fprintf(f, "#define arch_sizeof_double %d\n", sizeof(double));
  74.  
  75.     fprintf(f, "\n\t/* Unsigned max values */\n");
  76. #define pmax(str, typ, tstr, l)\
  77.   fprintf(f, "#define arch_max_%s ((%s)0x%s%s + (%s)0)\n",\
  78.     str, tstr, ffs + ffs_strlen - sizeof(typ) * 2, l, tstr)
  79.     pmax("uchar", unsigned char, "unsigned char", "");
  80.     pmax("ushort", unsigned short, "unsigned short", "");
  81.     pmax("uint", unsigned int, "unsigned int", "");
  82.     pmax("ulong", unsigned long, "unsigned long", "L");
  83. #undef pmax
  84.  
  85.     fprintf(f, "\n\t/* Miscellaneous */\n");
  86.     fprintf(f, "#define arch_is_big_endian %d\n", 1 - *(char *)&one);
  87.     pl0.l = 0;
  88.     pl1.l = -1;
  89.     fprintf(f, "#define arch_ptrs_are_signed %d\n",
  90.         (pl1.p < pl0.p));
  91.     f0.f = 0.0, f1.f = 1.0, fm1.f = -1.0;
  92.     /* We have to test the size dynamically here, */
  93.     /* because the preprocessor can't evaluate sizeof. */
  94.     fprintf(f, "#define arch_floats_are_IEEE %d\n",
  95.         ((sizeof(float) == sizeof(int) ?
  96.           f0.i == 0 && f1.i == (int)0x3f800000 && fm1.i == (int)0xbf800000 :
  97.           f0.l == 0 && f1.l == 0x3f800000L && fm1.l == 0xbf800000L)
  98.          ? 1 : 0));
  99.     /* There are three cases for arithmetic right shift: */
  100.     /* always correct, correct except for right-shifting a long by 1 */
  101.     /* (a bug in some versions of the Turbo C compiler), and */
  102.     /* never correct. */
  103.     ars = (lr2 != -1 || ir1 != -1 || ir2 != -1 ? 0 :
  104.         lr1 != -1 ? 1 :        /* Turbo C problem */
  105.         2);
  106.     fprintf(f, "#define arch_arith_rshift %d\n", ars);
  107.     /* Some machines can't handle a variable shift by */
  108.     /* the full width of a long. */
  109.     fprintf(f, "#define arch_can_shift_full_long %d\n",
  110.         um1 >> lwidth == 0);
  111.     fclose(f);
  112.     return 0;
  113. }
  114.